package com.cashbook.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.cashbook.mapper.TbUserMapper;
import com.cashbook.pojo.TbAdmin;
import com.cashbook.pojo.TbUser;
import com.cashbook.pojo.TbUserExample;
import com.cashbook.pojo.TbUserExample.Criteria;
import com.cashbook.pojo.TbUser;
import com.cashbook.service.UserService;

import entity.PageResult;

/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private TbUserMapper userMapper;

	@Override
	public List<TbUser> findAll() {
		return userMapper.selectByExample(null);
	}

	@Override
	public PageResult findPage(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);		
		Page<TbUser> page=   (Page<TbUser>) userMapper.selectByExample(null);
		return new PageResult(page.getTotal(), page.getResult());
		
	}

	@Override
	public void add(TbUser user) {
		userMapper.insert(user);
		
	}

	@Override
	public void update(TbUser user) {
		userMapper.updateByPrimaryKey(user);
		
	}

	@Override
	public TbUser findOne(Long id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			userMapper.deleteByPrimaryKey(id);
		}
		
	}

	@Override
	public PageResult findPage(TbUser user, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbUserExample example=new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(user!=null) {
			if(user.getAvatarUrl()!=null && user.getAvatarUrl().length()>0) {
				criteria.andAvatarUrlLike("%"+user.getAvatarUrl()+"%");
			}
			if(user.getGender()!=null) {
				criteria.andGenderEqualTo(user.getGender());
			}
			if(user.getHref()!=null && user.getHref().length()>0) {
				criteria.andHrefLike("%"+user.getHref()+"%");
			}
			if(user.getNickName()!=null && user.getNickName().length()>0) {
				criteria.andNickNameLike("%"+user.getNickName()+"%");
			}
			if(user.getOpenId()!=null && user.getOpenId().length()>0) {
				criteria.andOpenIdLike("%"+user.getOpenId()+"%");
			}
			if(user.getUnionId()!=null && user.getUnionId().length()>0) {
				criteria.andUnionIdLike("%"+user.getUnionId()+"%");
			}
		}
		Page<TbUser> page= (Page<TbUser>)userMapper.selectByExample(example);		
		return new PageResult(page.getTotal(), page.getResult());
	}

	@Override
	public List<TbUser> findByNickName(String nickName, Long gender) {
		TbUserExample example=new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(!"".equals(nickName) && nickName!=null) {
			criteria.andNickNameLike("%"+nickName+"%");
		}
		if(gender!=null) {
			criteria.andGenderEqualTo(gender);
		}
		return  userMapper.selectByExample(example);
	}

	@Override
	public List<TbUser> findByOpenId(String openId) {
		TbUserExample example=new TbUserExample();
		Criteria criteria = example.createCriteria();
		if(!"".equals(openId)&&openId!=null) {
			criteria.andOpenIdLike("%"+openId+"%");
		}
		return userMapper.selectByExample(example);
	}
	
	
}
